package net.a6polo.a6poloweibo;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class DetailTabActivity2 extends FragmentActivity {

	private String weiboId = "";

	private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_tab_activity2);

		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
		
		Intent intent = getIntent();
		weiboId = intent.getStringExtra("weiboId");

		// For each of the sections in the app, add a tab to the action bar.
		actionBar.addTab(actionBar.newTab().setText(R.string.activity_weibo_content).setTabListener(new TabListener<WeiboDetailTab>(this, "weiboTab", WeiboDetailTab.class)));
		actionBar.addTab(actionBar.newTab().setText(R.string.activity_weibo_comment).setTabListener(new TabListener<CommentTab>(this, "commentTab", CommentTab.class)));
		actionBar.addTab(actionBar.newTab().setText(R.string.activity_weibo_redirect).setTabListener(new TabListener<RepostTab>(this, "reposttTab", RepostTab.class)));
	}

	@Override
	public void onRestoreInstanceState(Bundle savedInstanceState) {
		if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
			getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.actionbar_weibo_detail, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {

		case android.R.id.home:
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.menu_repost:
			Intent repostIntent = new Intent(this, CommentWeiboactivity.class);
			repostIntent.putExtra("sourceId", weiboId);
			repostIntent.putExtra("type", "repost");
			startActivity(repostIntent);
			return true;
		case R.id.menu_comment:
			Intent commentIntent = new Intent(this, CommentWeiboactivity.class);
			commentIntent.putExtra("sourceId", weiboId);
			commentIntent.putExtra("type", "comment");
			startActivity(commentIntent);
			return true;

		default:
			return super.onOptionsItemSelected(item);

		}

	}

	public class TabListener<T extends Fragment> implements ActionBar.TabListener {

		private Fragment mFragment;
		private final Activity mActivity;
		private final String mTag;
		private final Class<T> mClass;

		public TabListener(Activity activity, String tag, Class<T> clz) {
			mActivity = activity;
			mTag = tag;
			mClass = clz;
		}

		@Override
		public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
			mFragment = Fragment.instantiate(mActivity, mClass.getName());
			
			Bundle bundle = new Bundle();  
            bundle.putString("weiboId", weiboId);  
			mFragment.setArguments(bundle);
			
            getSupportFragmentManager().beginTransaction()
            .replace(R.id.container, mFragment)
            .commit();

            
            
 
		}

		@Override
		public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
			// TODO Auto-generated method stub

		}

	}
}
